package com.waves.task.service.impl;

import org.apache.tomcat.util.threads.TaskQueue;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;

import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {
    public static void main(String[] args) {
        // 阻塞队列使用Tomcat的TaskQueue
        TaskQueue queue = new TaskQueue();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 5, 50, TimeUnit.SECONDS, queue);
        // 需要设置parent引用
        queue.setParent(executor);
        for (int i = 0; i < 10; i++) {
            executor.execute(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {}
            });
        }
        System.out.println("active: " + executor.getActiveCount());
        System.out.println("queue: " + executor.getQueue().size());
    }
}